package br.edu.ufra.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSession {

	private static SessionFactory sessionFactory = null;

	public static Configuration cfg;

	public static Configuration getConfiguration() {
		cfg = new Configuration();
		try {
			cfg.configure("/hibernate.cfg.xml");
		} catch (HibernateException e) {
			return null;
		}
		return cfg;

	}
	
	public static void setSessionFactory(SessionFactory fact) {
		sessionFactory = fact;
	}
	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			cfg = getConfiguration();
			sessionFactory = cfg.buildSessionFactory();
		}
		return sessionFactory;
	}
	
	public static Session getSession() throws HibernateException{
		return getSessionFactory().openSession();
	}
}